package com.cfx.exercise_42;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.cfx.exercise_46.HeaderAndFooter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class PDF {

	Document doc = new Document();
	PdfWriter pw;
	public static void main(String[] args) throws DocumentException, Exception {
		PDF pdf = new PDF();
		String[] string = new String[] { "张三的名字够长的测试专用章", "测试专用章", "椭圆形40" };
		List<String[]> list = new ArrayList<>();
		for(int i =0;i<100;i++){
			list.add(string);
		}
		//String[] s = new String[]{"这一行字数少一点","这一个可以长","圆形42"};
		/*for (int i = 0; i < 1; i++) {
			if(i%2==0){
				list.add(string);
			}else{				
				list.add(s);
			}
		}*/
		String[] string2 = new String[] { "北京金信科技股份有限公司", "北京市昌平区回龙观东大街118号", "010-1234567" };
		List<String[]> list2 = new ArrayList<>();
		for (int i = 0; i < 3; i++) {
			list2.add(string2);
		}

		pdf.getKFXPDF("1234", "1234", "2341", "zhangsan", "lisi", "65464321154", "12312", "cfx", "zs1990", list,
				list2);
	}
	private ByteArrayOutputStream ba;
	private PdfWriter pdfWriter;

	// private static Font headfont;// 设置字体大小
	private static Font titleFont;// 设置字体大小
	private static Font textFont;// 设置字体大小
	private static Font keyFont;

	public PDF(File file) {
		
		doc.setPageSize(PageSize.A4);// 设置页面大小
		try {
			pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(file));
			doc.open();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public ByteArrayOutputStream getBa() {
		return ba;
	}

	public void setBa(ByteArrayOutputStream ba) {
		this.ba = ba;
	}

	public PDF () {
		try {
			
			HeaderFooter footer = new HeaderFooter(new Phrase("第 ",keyFont), new Phrase(" 页",keyFont));
			footer.setAlignment(Element.ALIGN_CENTER);
			pw = PdfWriter.getInstance(doc, new FileOutputStream("pdf/example.pdf"));
			footer.setBorder(Rectangle.NO_BORDER);
			doc.setFooter(footer);
		
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}
		ba = new ByteArrayOutputStream();
		doc.setPageSize(PageSize.A4);// 设置页面大小
		doc.setMargins(50, 50, 50, 50);
		try {
			pdfWriter = PdfWriter.getInstance(doc, ba);
			doc.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static {
		BaseFont bfChinese;
		try {

			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, true);
			// headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
			titleFont = new Font(bfChinese, 20, Font.BOLD);// 设置字体大小
			textFont = new Font(bfChinese, 12, Font.NORMAL);// 设置字体大小
			keyFont = new Font(bfChinese, 13, Font.NORMAL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getKFXPDF(String code, String time, String companName, String leaderName, String agentName,
			String agentPhone, String count, String policeName, String activePwd, List<String[]> sealList,
			List<String[]> factoryList) throws Exception {
		Paragraph title = new Paragraph("黑河市公安局印章准刻通知书", titleFont);
		title.setAlignment(Element.ALIGN_CENTER);// 设置居中
		PdfPTable table = new PdfPTable(1);// 外table
		PdfPCell cell = new PdfPCell();
		cell.setBorderWidth(1f);
		table.setWidthPercentage(100f);
		table.setSplitLate(false);// 自动换页
		PdfPTable pt = new PdfPTable(2);
		pt.setSpacingBefore(40f);
		pt.setWidthPercentage(100f);
		pt.setWidths(new float[] { 50f, 50f });
		PdfPCell c1 = new PdfPCell(new Paragraph("批单编号：" + code, keyFont));
		c1.setBorder(0);
		c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		PdfPCell c2 = new PdfPCell(new Paragraph("审批日期：" + time, keyFont));
		c2.setBorder(0);
		c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
		pt.addCell(c1);
		pt.addCell(c2);
		// codeAndTime.setLeading(50f);
		// ///////////////////////////////
		PdfPTable t1 = new PdfPTable(3);
		t1.setWidthPercentage(100f);
		t1.setWidths(new float[] { 24f, 24f, 24f });
		PdfPCell t1c1 = new PdfPCell(new Paragraph("申刻单位：" + companName, keyFont));
		t1c1.setColspan(3);
		t1c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		t1c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c1.setFixedHeight(20f);
		t1c1.setBorder(0);
		PdfPCell t1c2 = new PdfPCell(new Paragraph("法定代表：" + leaderName, keyFont));
		t1c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c2.setHorizontalAlignment(Element.ALIGN_LEFT);
		t1c2.setFixedHeight(20f);
		t1c2.setBorder(3);
		PdfPCell t1c3 = new PdfPCell(new Paragraph("经办人：" + agentName, keyFont));
		t1c3.setHorizontalAlignment(Element.ALIGN_LEFT);
		t1c3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c3.setBorder(3);
		PdfPCell t1c4 = new PdfPCell(new Paragraph("联系电话：" + agentPhone, keyFont));
		t1c4.setHorizontalAlignment(Element.ALIGN_LEFT);
		t1c4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c4.setBorder(3);
		t1.addCell(t1c1);
		t1.addCell(t1c2);
		t1.addCell(t1c3);
		t1.addCell(t1c4);
		// ///////////////////////////
		PdfPTable t2 = new PdfPTable(4);
		t2.getDefaultCell().setMinimumHeight(30);
		t2.setWidthPercentage(100f);
		t2.setWidths(new float[] { 3f, 35f, 15f, 10f });
		PdfPCell t2c1 = new PdfPCell(new Paragraph("批\n\n准\n\n刻\n\n制\n\n印\n\n章", keyFont));
		t2c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		t2c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c1.setRowspan(sealList.size() + 2);
		t2c1.setBorder(8);
		PdfPCell t2c2 = new PdfPCell(new Paragraph("印章全称", keyFont));
		t2c2.setHorizontalAlignment(Element.ALIGN_LEFT);
		t2c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c2.setFixedHeight(20f);
		t2c2.setBorder(6);
		// t2c2.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		PdfPCell t2c3 = new PdfPCell(new Paragraph("印章类型", keyFont));
		t2c3.setHorizontalAlignment(Element.ALIGN_LEFT);
		t2c3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c3.setBorder(6);
		// t2c3.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		PdfPCell t2c4 = new PdfPCell(new Paragraph("印章规格", keyFont));
		t2c4.setHorizontalAlignment(Element.ALIGN_LEFT);
		t2c4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c4.setBorder(6);
		// t2c4.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		t2.addCell(t2c1);
		t2.addCell(t2c2);
		t2.addCell(t2c3);
		t2.addCell(t2c4);

		int k = 0;
		for (String[] strings : sealList) {
			k++;
			for (int i = 0; i < strings.length; i++) {
				PdfPCell pc = new PdfPCell(new Paragraph(strings[i], keyFont));
				pc.setHorizontalAlignment(Element.ALIGN_LEFT);
				pc.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
				//pc.setFixedHeight(20f);
				pc.setMinimumHeight(20f);
				pc.setBorder(0);
				if (k == sealList.size()) {		
					pc.setHorizontalAlignment(Element.ALIGN_LEFT);
					pc.setVerticalAlignment(PdfPCell.ALIGN_TOP);
					switch (k) {
					case 1:
						pc.setFixedHeight(200);
						break;
					case 2:
						pc.setFixedHeight(180);
						break;
					case 3:
						pc.setFixedHeight(160);
						break;
					case 4:
						pc.setFixedHeight(140);
						break;
					case 5:
						pc.setFixedHeight(120);
						break;
					case 6:
						pc.setFixedHeight(100);
						break;
					case 7:
						pc.setFixedHeight(80);
						break;
					case 8:
						pc.setFixedHeight(60);
						break;
					case 9:
						pc.setFixedHeight(40);
						break;	
					default:
						break;
					}
				}
				t2.addCell(pc);
			}
		}

		PdfPTable t3 = new PdfPTable(2);
		t3.setWidthPercentage(100f);
		t3.setWidths(new float[] { 20f, 20f });
		PdfPCell t3c1 = new PdfPCell(new Paragraph("经办人签名：", keyFont));
		t3c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		t3c1.setBorder(1);
		t3c1.setFixedHeight(40f);
		PdfPCell t3c2 = new PdfPCell(new Paragraph("审批单位（盖章有效）：" + policeName, keyFont));
		t3c2.setHorizontalAlignment(Element.ALIGN_LEFT);
		t3c2.setBorder(1);
		PdfPCell t3c3 = new PdfPCell(new Paragraph("承接密码：" + activePwd, keyFont));
		t3c3.setHorizontalAlignment(Element.ALIGN_LEFT);
		t3c3.setBorder(0);
		t3c3.setFixedHeight(40f);
		PdfPCell t3c4 = new PdfPCell(new Paragraph("审批人签名：", keyFont));
		t3c4.setHorizontalAlignment(Element.ALIGN_LEFT);
		t3c4.setBorder(0);

		t3.addCell(t3c1);
		t3.addCell(t3c2);
		t3.addCell(t3c3);
		t3.addCell(t3c4);
		cell.addElement(t1);
		cell.addElement(t2);
		cell.addElement(t3);
		table.addCell(cell);
		Paragraph text = new Paragraph(
				"\n注意事项：\n1.\t批单打印后，需审批单位盖章有效，有效期为5个工作日，请在" + "有效期内持有该通知书到黑\t   河市有资质的公章刻制企业进行印章刻制，逾期须重新申请；\n"
						+ "2.\t公章刻制企业将印章信息（印章编码、章面内容、审批地点等公开信息及加密信息）导入\t   印章章体内的防伪芯片，芯片的内容可以用专门读写设备读取。\n\n",
				textFont);
		PdfPTable t = new PdfPTable(3);
		float[] width = { 13f, 15f, 6f };
		t.setWidths(width);
		t.setWidthPercentage(100);
		PdfPCell tp1 = new PdfPCell(new Paragraph("企业名称:",keyFont));
		PdfPCell tp2 = new PdfPCell(new Paragraph("企业地址:",keyFont));
		PdfPCell tp3 = new PdfPCell(new Paragraph("企业电话:",keyFont));
		tp1.setBorder(0);
		tp2.setBorder(0);
		tp3.setBorder(0);
		tp1.setMinimumHeight(20f);
		tp2.setMinimumHeight(20f);
		tp3.setMinimumHeight(20f);
		t.addCell(tp1);
		t.addCell(tp2);
		t.addCell(tp3);
		/*
		 * for (int i = 0; i < 3; i++) { Paragraph p1 = new Paragraph("企业名称：",
		 * textFont); p1.setAlignment(0); PdfPCell ca = new PdfPCell(p1);
		 * ca.setHorizontalAlignment(Element.ALIGN_LEFT);
		 * ca.setVerticalAlignment(Element.ALIGN_CENTER); ca.setBorder(0);
		 * PdfPCell cb = new PdfPCell(new Paragraph("企业地址：", textFont));
		 * cb.setBorder(0); PdfPCell cc = new PdfPCell(new Paragraph("企业电话：",
		 * textFont)); cc.setBorder(0); t.addCell(ca); t.addCell(cb);
		 * t.addCell(cc); }
		 */
		for (String[] strings : factoryList) {
			for (int i = 0; i < strings.length; i++) {
				PdfPCell pc;
				if (i == 0) {
					pc = new PdfPCell(new Paragraph( strings[i], keyFont));
				} else if (i == 1) {
					pc = new PdfPCell(new Paragraph( strings[i], keyFont));
				} else {
					pc = new PdfPCell(new Paragraph( strings[i], keyFont));
				}

				pc.setHorizontalAlignment(Element.ALIGN_LEFT);
				pc.setVerticalAlignment(Element.ALIGN_CENTER);
				// pc1.setFixedHeight(20f);
				pc.setBorder(0);
				
				t.addCell(pc);
			}
		}
		doc.add(title);
		doc.add(pt);
		doc.add(table);
		doc.add(text);
		doc.add(t);
		doc.close();
	}
	public void onEndPage (PdfWriter writer, Document document){
		
		 Rectangle rect = writer.getBoxSize("art");
		    switch(writer.getPageNumber() % 2) {
		    case 0:
		    	ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_RIGHT, new Phrase("even header"),
		                rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0);
		        break;
		    case 1:
		        ColumnText.showTextAligned(writer.getDirectContent(),
		                Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
		                300f, 62f, 0);
		        break;
		    }
	}
}
